package com.badibadi.activity;

import com.badibadi.adapter.BeforeLandingPageAdapter;
import com.badibadi.fragment.ActivityMyActivityFragment0;
import com.badibadi.fragment.ActivityOverViewFragment0;
import com.badibadi.fragment.AfterLandingMyHomePageClubManangerFragment;
import com.badibadi.fragment.AfterLandingMyHomePageMyFragment;
import com.badibadi.fragment.ClubOverview0Fragmente;
import com.badibadi.fragment.ShouYeHuoDongFragment;
import com.badibadi.fragment.ShouYeJuLeBuFragment;
import com.badibadi.fragment.UpdateManager;
import com.badibadi.fragment.WaterFallFragment;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.DisplayUtil;
import com.badibadi.mytools.TabHostUtils;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.view.my_view.SlideMenu;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.Toast;

//   
public class BeforeLandingPageActivity extends BaseActivity implements
		OnClickListener {
	private SlideMenu slideMenu;//slide_menu

	private Intent intent = null;
	private LinearLayout ll_layout;
	private ListView lv;
	private Button before_index;

	private TabHost mTabHost;
	public static BeforeLandingPageActivity instants;
	private FragmentManager manager;
	private Bundle bun = null;
	private WaterFallFragment fragment0;
	//改
	private ShouYeHuoDongFragment fragment1;
	
	private ShouYeJuLeBuFragment fragment2;
	private AfterLandingMyHomePageClubManangerFragment afterLandingMyHomePageClubManangerFragment;
	private AfterLandingMyHomePageMyFragment afterLandingMyHomePageMyFragment;
	/** 首页瀑布流 10 **/
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				finish();
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.beforelandingpage_activity_main);
		instants = this;
		manager = getSupportFragmentManager();
		init();
		initTabHost();
		//    

	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Constants.Before_handler = handler;
	}

	private void initTabHost() {
		// TODO Auto-generated method stub
		mTabHost = (TabHost) this.findViewById(R.id.before_landing_tabhost);
		mTabHost.setup();
		mTabHost.addTab(mTabHost
				.newTabSpec("before_landing_tabhost one")
				.setIndicator(getResources().getString(R.string.newest_moment),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab1));
		mTabHost.addTab(mTabHost
				.newTabSpec("before_landing_tabhost two")
				.setIndicator(getResources().getString(R.string.wx_activity),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab2));
		mTabHost.addTab(mTabHost
				.newTabSpec("before_landing_tabhost three")
				.setIndicator(getResources().getString(R.string.wx_club),
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.tab3));
		TabHostUtils.SetTabHost(this, mTabHost);
		mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				// Toast.makeText(getApplicationContext(),
				// "" + mTabHost.getCurrentTab(), 0).show();
				switch (mTabHost.getCurrentTab()) {
				case 0:
					LoadFragment(0);
					break;
				case 1:
					LoadFragment(1);
					break;
				case 2:
					LoadFragment(2);
					break;
				}
			}
		});
	}

	protected void LoadFragment(int num) {
		bun = new Bundle();
		switch (num) {
		case 0:
			FragmentTransaction transaction = manager.beginTransaction();
			fragment0 = WaterFallFragment.getInstance();
			bun.putInt("type", 3);
			fragment0.setArguments(bun);
			transaction.replace(R.id.before_landing_right_layout, fragment0);
			transaction.commit();
			break;
		case 1:
			FragmentTransaction transaction2 = manager.beginTransaction();
			fragment1 =new  ShouYeHuoDongFragment();
			bun = new Bundle();	
			bun.putString("uid", "1");
			bun.putInt("type", 0);
			fragment1.setArguments(bun);			
			transaction2.replace(R.id.before_landing_right_layout, fragment1);
			transaction2.commit();
			break;
		case 2:
			FragmentTransaction transaction3 = manager.beginTransaction();
			fragment2 = new ShouYeJuLeBuFragment();
			bun = new Bundle();
			bun.putString("type", "0");
			fragment2.setArguments(bun);
			transaction3.replace(R.id.before_landing_right_layout, fragment2);
			transaction3.commit();
			break;
		}
	}

	private void init() {
		// TODO Auto-generated method stub
		
		slideMenu = (SlideMenu) findViewById(R.id.slide_menu);
		slideMenu.setAfter(false);
		LoadFragment(0);
		afterLandingMyHomePageClubManangerFragment = new AfterLandingMyHomePageClubManangerFragment();
		afterLandingMyHomePageMyFragment = new AfterLandingMyHomePageMyFragment();
		ImageView menuImg = (ImageView) findViewById(R.id.title_bar_menu_btn);
		menuImg.setOnClickListener(this);
		before_index = (Button) findViewById(R.id.before_index);
		before_index.setOnClickListener(this);

		lv = (ListView) this.findViewById(R.id.before_landing_left_listView);
		BeforeLandingPageAdapter bAdapter = new BeforeLandingPageAdapter(
				BeforeLandingPageActivity.this);
		ll_layout = (LinearLayout) this.findViewById(R.id.ll_layout);

		// LinearLayout.LayoutParams ll=new
		// LinearLayout.LayoutParams(355,LayoutParams.MATCH_PARENT);
		// lv.setLayoutParams(ll);
		lv.setAdapter(bAdapter);
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				// TODO Auto-generated method stub
				switch (position) {
				// 首页
				case 2:
					openOrCloseMenu();
					break;
				// 俱乐部列表
				case 3:
					intent = new Intent(BeforeLandingPageActivity.this,
							ClubListActivity.class);
					break;
				// 活动总览
				case 4:
					intent = new Intent(BeforeLandingPageActivity.this,
							ActivityOverViewActivity.class);
					break;
				// 每日一记
				case 5:
					intent = new Intent(BeforeLandingPageActivity.this,
							RecordAdayToRememberActivity.class);
					break;
				// 购买处
				case 6:
//					intent = new Intent(BeforeLandingPageActivity.this,
//							RechargeActivity.class);
					Utils.showMessage(BeforeLandingPageActivity.this, ""+getResources().getString(R.string.l_xb10));
					break;
				// 清除缓存
				case 8:
					try {
						ImageLoader.getInstance().clearDiscCache();
						ImageLoader.getInstance().clearMemoryCache();
						Thread.sleep(3000);
						Utils.showMessage(BeforeLandingPageActivity.this,
								getResources().getString(R.string.l_xa8));
					} catch (Exception e) {
						// TODO: handle exception
					}
					break;
				// 更新
				case 9:
					UpdateManager updateManager=new UpdateManager(BeforeLandingPageActivity.this);
					updateManager.getApkInfo();
					
					break;
				// 意见反馈
				case 10:
					intent = new Intent(BeforeLandingPageActivity.this,
							Fingerpost_Activity.class);
					intent.putExtra("panduan_id", "feedback");
					break;
//				// 语言切换
//				case 11:
//					new com.badibadi.mytools.Dialog()
//							.LanguageSwitching(BeforeLandingPageActivity.this);
//					break;
				case 11:// 使用帮助
					intent = new Intent(BeforeLandingPageActivity.this,
							Fingerpost_Activity.class);
					intent.putExtra("panduan_id", "Maps");
					break;
				case 12:// 关于我们
					intent = new Intent(BeforeLandingPageActivity.this,
							Fingerpost_Activity.class);
					intent.putExtra("panduan_id", "aboutUs");
					break;
				case 13:// 隐私声明
					intent = new Intent(BeforeLandingPageActivity.this,
							Fingerpost_Activity.class);
					intent.putExtra("panduan_id", "statement");
					break;

				}
				//

				if (intent != null) {
					startActivity(intent);
					intent = null;
				}
			}
		});

		// ListView lv2=new ListView(this);
		// lv2.setAdapter(new
		// BeforeLandingPageAdapter(BeforeLandingPageActivity.this));
		// LinearLayout
		// ll=(LinearLayout)this.findViewById(R.id.before_landing_right_layout);
		// ll.addView(lv2);

	}

	/**
	 * 是填满左边的部分，不然显示不完
	 */
	public void tianManLeftRongqi() {
		int winW = (int) getScreenSize(SCREENW);
		winW = winW - DisplayUtil.dip2px(BeforeLandingPageActivity.this, 50);
		ll_layout.setLayoutParams(new android.view.ViewGroup.LayoutParams(winW,
				android.view.ViewGroup.LayoutParams.MATCH_PARENT));
		lv.setLayoutParams(new LinearLayout.LayoutParams(winW,
				LinearLayout.LayoutParams.MATCH_PARENT));
	}

	// private void setWaterFall() {
	// // TODO Auto-generated method stub
	// ViewFragmentHolder holder = new ViewFragmentHolder();
	// WaterFallFragment warterfragment = new WaterFallFragment();
	// holder.transaction.replace(R.id.before_landing_right_layout,
	// warterfragment);
	// holder.transaction.commit();
	// }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_bar_menu_btn:
			
			if (slideMenu.isMainScreenShowing()) {
				slideMenu.openMenu();
			} else {
				slideMenu.closeMenu();
			}
			break;
		case R.id.before_index:
			
				startActivity(new Intent(BeforeLandingPageActivity.this,
						IndexActivity.class));
			

			break;
		}
	}

	/**
	 * 打开或者关闭抽屉式菜单
	 */
	private void openOrCloseMenu() {
		if (slideMenu.isMainScreenShowing()) {
			slideMenu.openMenu();
		} else {
			slideMenu.closeMenu();
		}
	}
}
